package servlet;

import dao.OperatorDAOImpl;
import pojo.Operator;
import utils.ApiResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/operator")
public class OperatorServlet extends BaseServlet {

    // 添加操作员
    public void addOperator(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int operatorID = Integer.parseInt(req.getParameter("operatorID"));
        String workArea = req.getParameter("workArea");
        int warehouseNumber = Integer.parseInt(req.getParameter("warehouseNumber"));
        String idNumber = String.valueOf(req.getParameter("idNumber"));
        int departmentID = Integer.parseInt(req.getParameter("departmentID"));

        Operator operator = new Operator(operatorID, workArea, warehouseNumber, idNumber, departmentID);
        OperatorDAOImpl.add(operator);
        sendJsonResponse(resp, new ApiResult(200, "Operator added successfully.", null));
    }

    // 删除操作员
    public void deleteOperator(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int operatorID = Integer.parseInt(req.getParameter("operatorID"));
        OperatorDAOImpl.delete(operatorID);
        sendJsonResponse(resp, new ApiResult(200, "Operator deleted successfully.", null));
    }

    // 更新操作员
    public void updateOperator(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int operatorID = Integer.parseInt(req.getParameter("operatorID"));
        String workArea = req.getParameter("workArea");
        int warehouseNumber = Integer.parseInt(req.getParameter("warehouseNumber"));
        int departmentID = Integer.parseInt(req.getParameter("departmentID"));
        String idNumber = String.valueOf(req.getParameter("idNumber"));

        Operator operator = new Operator(operatorID, workArea, warehouseNumber, idNumber, departmentID);
        OperatorDAOImpl.update(operator);
        sendJsonResponse(resp, new ApiResult(200, "Operator updated successfully.", null));
    }

    // 获取所有操作员
    public void getAllOperators(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Operator> operators = OperatorDAOImpl.getAll();
        sendJsonResponse(resp, new ApiResult(200, "Success", operators));
    }

    // 根据编号获取操作员
    public void getOperatorById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int operatorID = Integer.parseInt(req.getParameter("operatorID"));
        Operator operator = OperatorDAOImpl.getById(operatorID);
        sendJsonResponse(resp, new ApiResult(200, "Success", operator));
    }

    // 获取操作员数量
    public void getOperatorCount(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int count = OperatorDAOImpl.getCount();
        sendJsonResponse(resp, new ApiResult(200, "Success", count));
    }

    // 分页获取操作员
    public void getOperatorsByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Operator> operators = OperatorDAOImpl.getByPage(offset, pageSize);
        sendJsonResponse(resp, new ApiResult(200, "Success", operators));
    }

    // 根据关键字搜索操作员
    public void findOperatorsByKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        List<Operator> operators = OperatorDAOImpl.findByKeyword(keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", operators));
    }

    // 根据关键字分页搜索操作员
    public void findOperatorsByPageAndKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Operator> operators = OperatorDAOImpl.findByPageAndKeyword(offset, pageSize, keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", operators));
    }
}
